/**
 * 
 */
package wangjg.commons.character;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * @author wangjg
 *
 */
public class StringUtil {
	public static final String DATETIME_FORMAT_EN = "yyyy-MM-dd HH:mm:ss";
	public static final String DATETIME_FORMAT_CN = "yyyy年MM月dd日 HH点mm分ss秒";
	
	public static Date parseDate(String dateStr) {
		String format = DATETIME_FORMAT_EN.substring(0, dateStr.length());
		java.text.DateFormat df = new java.text.SimpleDateFormat(format);
		try {
			return df.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static String formatDate(Date date) {
		String datestr = "";
		try {
			java.text.DateFormat df = new java.text.SimpleDateFormat(DATETIME_FORMAT_EN);
			datestr = df.format(date);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return datestr;
	}
	
	public static Date parseGMTDate(String dateStr) {
		try {
			DateFormat gmt = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z",Locale.ENGLISH);
			return gmt.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/***************************************************************************
	 * 替换字符串适用于jdk1.4之前；之后可用String.replaceALL(); *
	 **************************************************************************/
	public static String replace(String parentStr, String ch, String rep) {
		int i = parentStr.indexOf(ch);
		StringBuffer sb = new StringBuffer();
		if (i == -1)
			return parentStr;
		sb.append(parentStr.substring(0, i) + rep);
		if (i + ch.length() < parentStr.length())
			sb.append(replace(parentStr.substring(i + ch.length(), parentStr
					.length()), ch, rep));
		return sb.toString();
	}

	/***************************************************************************
	 * 替换字符串（不分大小写） *
	 **************************************************************************/
	/**
	 * Replaces all instances of oldString with newString in line with the added
	 * feature that matches of newString in oldString ignore case.
	 * 
	 * @param line the String to search to perform replacements on
	 * @param oldString the String that should be replaced by newString
	 * @param newString the String that will replace all instances of oldString
	 * 
	 * @return a String will all instances of oldString replaced by newString
	 */
	public static final String replaceIgnoreCase(String line, String oldString,
			String newString) {
		if (line == null) {
			return null;
		}
		String lcLine = line.toLowerCase();
		String lcOldString = oldString.toLowerCase();
		int i = 0;
		if ((i = lcLine.indexOf(lcOldString, i)) >= 0) {
			char[] line2 = line.toCharArray();
			char[] newString2 = newString.toCharArray();
			int oLength = oldString.length();
			StringBuffer buf = new StringBuffer(line2.length);
			buf.append(line2, 0, i).append(newString2);
			i += oLength;
			int j = i;
			while ((i = lcLine.indexOf(lcOldString, i)) > 0) {
				buf.append(line2, j, i - j).append(newString2);
				i += oLength;
				j = i;
			}
			buf.append(line2, j, line2.length - j);
			return buf.toString();
		}
		return line;
	}
	
	public static String htmlEncode(String text) {
		if(text==null||text.length()==0){
			return text;
		}
		StringBuilder sb = new StringBuilder();
		int len = text.length();
		for(int i=0;i<len;i++){
			char c = text.charAt(i);
			switch (c) {
			case '&':
				sb.append("&amp;");
				break;
			case '<':
				sb.append("&lt;");
				break;
			case '>':
				sb.append("&gt;");
				break;
			case '\'':
				sb.append("&apos;");
				break;
			case '"':
				sb.append("&quot;");
				break;
			default:
				sb.append(c);
			}
		}
		return sb.toString();
	}
	
	//?
	public static String decodeUnicode(String s){
		if (s == null || s.length()==0)
			return "";
		StringBuffer sb = new StringBuffer();
		boolean escape = false;
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			switch (c) {
			case '\\':
			case '%':
				escape = true;
				break;
			case 'u':
			case 'U':
				if (escape) {
					sb.append((char) Integer.parseInt(s.substring(i + 1,i + 5), 16));
					escape = false;
					i += 4;
				} else {
					sb.append(c);
				}
				break;
			default:
				sb.append(c);
				break;
			}
		}
		return sb.toString();
	}

	/**
	 * 检查正则匹配
	 */
	public static boolean checkPattern(String regEx, String str) {
		java.util.regex.Pattern p = java.util.regex.Pattern.compile(regEx);
		java.util.regex.Matcher m = p.matcher(str);
		return m.find();
	}
	
}
